<?php
class Item_model extends CI_Model{
	function __construct(){
		parent::__construct();
	}

	public function get_all(){
		$query = $this->db->get('item');
		return $query->result();
	}

	public function get_updated($day){

		$this->db->where('item_modified_date >= DATE_SUB(NOW(), INTERVAL '.$day.' DAY)');
		$query = $this->db->get('item');
		return $query->result();
	}

	public function add($name, $barcode, $type, $category_id, $manufacturer_id, $cost, $price, $active){
		$data = array(
				'item_name' => utf8_encode($name) ,
				'item_barcode' => utf8_encode($barcode) ,
				'item_type_id' => $type,
				'category_id' => $category_id,
				'manufacturer_id' => $manufacturer_id,
				'item_cost' => $cost,
				'item_price' => $price,
				'item_active' => $active
		);
		if ($this->db->insert('item', $data))
			return $this->db->insert_id();
		else
			return -1;
	}

	public function get_item_by_barcode($barcode){
		$query = $this->db->get_where('item', array('item_barcode' => $barcode));
		if ($query->num_rows() > 0)
		{
			$row = $query->row();
			return $row;
		}else
			return null;
	}
	
	public function get_low_stock_item(){
		$this->db->select('*');
		$this->db->from('item');
		$this->db->join('inventory', 'inventory.item_id = item.item_id');
		$this->db->where('inventory_min_stock >= inventory_current_stock');
		$this->db->where('item_active', 1);
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}else
			return null;
	}

	public function get_item_by_id($id){

		$this->db->select('item.item_id, item_name,item_cost,item_barcode, item_price, item_type_id, manufacturer_name, category_name, inventory_current_stock, inventory_min_stock, promotion_unit, promotion_discount, promotion_active, item_active');
		$this->db->from('item');
		$this->db->join('manufacturer', 'manufacturer.manufacturer_id = item.manufacturer_id');
		$this->db->join('category', 'category.category_id = item.category_id');
		$this->db->join('inventory', 'inventory.item_id = item.item_id');
		$this->db->join('promotion', 'promotion.item_id = item.item_id', 'left');
		$this->db->where('item.item_id', $id);
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->row();
		}
		return null;
	}

	public function search($string, $name = 1, $barcode = 1, $category = 0, $manufacturer, $limit =30, $start = 0){
		//if (strlen($string)> 0 ){
		$this->db->select('item.item_id, item_name,item_cost,item_barcode,item_price, manufacturer_name, category_name, inventory_current_stock, inventory_min_stock, promotion_unit, promotion_discount, promotion_active, item_active');
		$this->db->from('item');
		$this->db->join('manufacturer', 'manufacturer.manufacturer_id = item.manufacturer_id');
		$this->db->join('category', 'category.category_id = item.category_id');
		$this->db->join('inventory', 'inventory.item_id = item.item_id');
		$this->db->join('promotion', 'promotion.item_id = item.item_id', 'left');
		$this->db->limit($limit, $start);
		if ($name){
			$this->db->or_like('item_name', $string, 'after');
			$this->db->or_like('item_name', ' '.$string);
		}
		if ($barcode){
			$this->db->or_like('item_barcode', $string);
		}
		if ($category){
			$this->db->or_like('category_name', $string, 'after');
			$this->db->or_like('category_name', ' '.$string);
		}
		if ($manufacturer){
			$this->db->or_like('manufacturer_name', $string, 'after');
			$this->db->or_like('manufacturer_name', ' '.$string);
		}
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		//}
		return null;
	}
	
	public function count_search($string, $name, $barcode, $category, $manufacturer){
		//if (strlen($string)> 0 ){
		$this->db->select('item.item_id, item_name,item_cost,item_barcode,item_price, manufacturer_name, category_name, inventory_current_stock, inventory_min_stock, promotion_unit, promotion_discount, promotion_active, item_active');
		$this->db->from('item');
		$this->db->join('manufacturer', 'manufacturer.manufacturer_id = item.manufacturer_id');
		$this->db->join('category', 'category.category_id = item.category_id');
		$this->db->join('inventory', 'inventory.item_id = item.item_id');
		$this->db->join('promotion', 'promotion.item_id = item.item_id', 'left');
		if ($name){
			$this->db->or_like('item_name', $string, 'after');
			$this->db->or_like('item_name', ' '.$string);
		}
		if ($barcode){
			$this->db->or_like('item_barcode', $string);
		}
		if ($category){
			$this->db->or_like('category_name', $string, 'after');
			$this->db->or_like('category_name', ' '.$string);
		}
		if ($manufacturer){
			$this->db->or_like('manufacturer_name', $string, 'after');
			$this->db->or_like('manufacturer_name', ' '.$string);
		}

		return $this->db->count_all_results();
		
	}

	public function update($name, $barcode, $type, $category_id, $manufacturer_id, $cost, $price, $active){
		$item = array(
				'item_name' => $name,
				'item_type_id' => $type,
				'category_id' => $category_id,
				'manufacturer_id' => $manufacturer_id,
				'item_cost' => $cost,
				'item_price' => $price,
				'item_active' => $active,
		);
		$this->db->where('item_barcode', $barcode);
		if ($this->db->update('item', $item))
			return true;
		else
			return false;
	}
}